﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Agile.Engine.Data
{
    /// <summary>
    /// 数据执行逻辑
    /// </summary>
    public class DataRule
    {
        #region Constructor

        public DataRule()
        {
            InitializeResult();
        }

        #endregion

        #region Properties
        /// <summary>
        /// 数据访问模型
        /// </summary>
        public DataModel Model { get; set; }

        /// <summary>
        /// 传入的参数
        /// </summary>
        public System.Collections.Hashtable Paramater { get; set; }
        /// <summary>
        /// 回传的结果
        /// </summary>
        public System.Collections.Hashtable Result { get; set; }

        /// <summary>
        /// 执行状态
        /// </summary>
        public int Status
        {
            get { return int.Parse(Result["__status"].ToString()); }
            set { Result["__status"] = value; }
        }

        /// <summary>
        /// 返回消息
        /// </summary>
        public string Message
        {
            get { return Result["__msg"].ToString(); }
            set { Result["__msg"] = value; }
        }

        protected virtual bool BeforeExecute()
        {
            return true;
        }

        #endregion

        #region Functions

        /// <summary>
        /// 回传的结果初始化
        /// </summary>
        void InitializeResult()
        {
            Result = new System.Collections.Hashtable();
            Status = 0;
            Message = string.Empty;
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        public virtual bool ValidateExecute()
        {
            return true;
        }

        /// <summary>
        /// 重载事务执行：验证，执行，设置返回值
        /// </summary>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public bool ExecuteTrans(System.Collections.Hashtable param)
        {
            Paramater = param;
            return ExecuteTrans();
        }

        /// <summary>
        /// 事务执行：验证，执行，设置返回值
        /// </summary>
        /// <returns></returns>
        public bool ExecuteTrans()
        {
            bool result = false;
            if (!BeforeExecute())
            {
                return false;
            }
            ///执行事务
            Model.ExecuteTransaction(
                () =>
                {
                    try
                    {
                        ///验证处理
                        if (ValidateExecute())
                        {
                            ///执行
                            ExecuteNonTrans();
                            ///设置返回结果
                            ResetResult();
                        }

                        result = true;
                    }
                    catch (Exception ex)
                    {
                        PrecessException(ex);
                        result = false;
                    }
                });

            return result;

        }

        /// <summary>
        /// 执行
        /// </summary>
        public virtual void ExecuteNonTrans() { }

        /// <summary>
        /// 处理异常
        /// </summary>
        /// <param name="ex"></param>
        public virtual void PrecessException(Exception ex)
        {
            Status = -1;
            Message = ex.Message;
        }

        /// <summary>
        /// 设置返回值
        /// </summary>
        public virtual void ResetResult() { }

        #endregion
    }

}
